package com.example.jdbcdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.*;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author zsf
 */
@RestController
@RequestMapping("/jdbc")
public class JdbcController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("/updateTest")
    public void updateTest() {
        StopWatch stopWatch = new StopWatch();

        stopWatch.start("查询任务");

        Object[] args = new Object[]{"EBECE14140D31B05E055000000000001",
                "EBECE14140D51B05E055000000000001", "EBECE14140D71B05E055000000000001"};
        String sql = "select a.* from trade_request a where a.id=?";
        List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, args);

        System.out.println(results.size());


        // List<String> ids = selectListByCursor(sql, String.class);
        //
        // stopWatch.stop();
        //
        // stopWatch.start("更新任务");
        //
        // List<CompletableFuture<Boolean>> futures = new ArrayList<>();
        //
        // List<List<String>> lists = splitList(ids, 20000);
        // for (List<String> list : lists) {
        //     CompletableFuture<Boolean> future = AsyncUtil.execAsync(() -> {
        //         //删除业务表数据
        //         batchInsert("bonus_detail", "bonus_detail_tmp", list, "id");
        //         return Boolean.TRUE;
        //     });
        //     futures.add(future);
        // }
        //
        // AsyncUtil.allOf(futures).join();
        //
        // stopWatch.stop();
        //
        // System.out.println(stopWatch.prettyPrint());
    }

    private void batchInsert(String sourceTable, String targetTable, List<String> idList, String pk) {
        String sql = "insert into " + targetTable + " select * from " + sourceTable + " where " + pk + "=?";
        List<Object[]> args = new ArrayList<>();
        for (String s : idList) {
            args.add(new Object[]{s});
        }
        System.out.println("sql:" + sql);
        jdbcTemplate.batchUpdate(sql, args);
    }

    private List<List<String>> splitList(Collection<String> list, int pageSize) {
        List<List<String>> result = new ArrayList<>();
        if (list.size() <= pageSize) {
            result.add(new ArrayList<>(list));
            return result;
        }
        List<String> tmp = new ArrayList<>(list);
        for (int i = 0, n = tmp.size(); i < n; i += pageSize) {
            int toIndex = i + pageSize > n ? n : i + pageSize;
            result.add(tmp.subList(i, toIndex));
        }
        return result;
    }

    private List<String> selectListByCursor(String sql, Class<String> clazz, Object... args) {
        boolean flag = true;
        if (String.class == clazz) {
            flag = false;
        }
        RowMapper<String> rowMapper = flag ? new BeanPropertyRowMapper<>(clazz) : new SingleColumnRowMapper<>(clazz);
        ResultSetExtractor<List<String>> rse = new RowMapperResultSetExtractor(rowMapper);

        return jdbcTemplate.query(con -> {
            con.setAutoCommit(false);
            PreparedStatement ps = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ps.setFetchSize(2000);
            ps.setFetchDirection(ResultSet.FETCH_FORWARD);
            for (int i = 1; i <= args.length; i++) {
                ps.setObject(i, args[i - 1]);
            }
            return ps;
        }, rse);
    }

}
